package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 上海热线新闻> 解析类 
 *  
 * @author KK
 *
 */
public class OnlineSHNewsAnalyse implements AnalyseNews {
	
	

	
	public boolean isDetailPage(String url) {
		String[] regex={
				"http://.*.online.sh.cn/.*content/[0-9]{4}\\-[0-9]{2}/[0-9]{2}/content_[0-9]*[_0-9]*.htm",
				
				};
		
		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);		
		doc.select("#displaypagenum").remove();
		String author=null;
		title=doc.select("div#headline").text();
		dateStr=doc.select("div#source").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#zoom.text"));		
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.two_left div.biaoti").text();
			dateStr=doc.select("div.two_left div.riqi").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.two_left div#zoom"));	
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("div.document h1").text();
			dateStr=url;
			content=HtmlCleaner.getContentHtml(url,doc.select("div.document div.doc_body"));	
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("h1").text();
			dateStr=doc.select("#source").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("#cont p"));	
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}
		if(StringUtils.isNullOrEmpty(title)){
			title=doc.select("h2").text();
			dateStr=doc.select("p.date").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.document"));	
		}
		
		if(dateStr!=null){
			date=DateUtils.matchDate(dateStr);	
		}		
		if(StringUtils.isBlank(title) || date ==null){
		}
			NewsMeta news=new NewsMeta();
			news.setUrl(url);		
			news.setType(1);		
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);		
			if(StringUtils.isBlank(author)){
			 author=doc.select("div#source").text();
			if(author!=null && author.indexOf("[来源]：")!=-1){
				author=StringUtils.substringAfter(author, "[来源]：");				
			}
			}
			news.setAuthor(author);
			return news;
	}
	
	

	public static void main(String[] args) {		
		OnlineSHNewsAnalyse a=new OnlineSHNewsAnalyse();
		String url="http://news.online.sh.cn/news/gb/content/2014-05/14/content_6862157_2.htm";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
